home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)TB / (A)TBR.ADF / CLI_Only / TinyType130.c < prev    next >
C/C++ Source or Header  |  1991-10-22  |  18KB  |  470 lines

  1. /*****************************************************************************\
  2. *                                                                             *
  3. *  TinyType.c V1.30                                                           *
  4. *                                                                             *
  5. *      By Christopher Weber   (c) 1991 - Freely distributable as long as the  *
  6. *                                        source code and documentation files  *
  7. *                                        are  kept  unaltered  and  together. *
  8. *                                                                             *
  9. *      Based on PinHead2 By Frank Ch. Eigler  (c) 1988                        *
  10. *                                                                             *
  11. *                                                                             *
  12. *                                                                             *
  13. *    This is  Not Shareware - it is free.                                     *
  14. *                                                                             *
  15. *    This is version 1.20 of TinyType.  This program is a modification of the *
  16. *    source for PinHead2 a wonderfully useful program written by Frank Ch.    *
  17. *    Eigler.  This program has one main advantage over PinHead2 - it allows   *
  18. *    the user to print either all the even numbered pages or all of the odd   *
  19. *    numbered pages at one time.                                              *
  20. *                                                                             *
  21. \*****************************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #define LINES   144                     /* No. of lines of text we'll print */
  27. #define COLUMNS 160                     /* No. of columns we'll print       */
  28.  
  29. /* Prototype functions used */
  30.  
  31. void PrintPage(void);                   /* Print a page                     */
  32. void ReadPage(void);                    /* Read in a page - based on format */
  33. void HelpTxt(void);                     /* Print Help text for user         */
  34.  
  35. char PText[LINES][COLUMNS + 1];         /* Actual buffer where we put the   */
  36.                                         /* text before we print it          */
  37.                                         /* Note - we won't use a 00 byte to */
  38.                                         /* terminate the strings.           */
  39.  
  40. short   Pages = 1;                      /* Page counter..                   */
  41. short   NumColumns;                     /* No. of columns to be formatted   */
  42. short   ColWidth;                       /* No. of chars per column          */
  43. short   TabSize;                        /* No. of spaces / tab              */
  44. short   PrintFooter;                    /* Whether or not to print footer   */
  45. short   StartPage;                      /* First page to print              */
  46. short   EndPage;                        /* Last page to print               */
  47. short   Query;                          /* Query mode flag                  */
  48. short   Odd;                            /* Odd page flag                    */
  49. short   Even;                           /* Even page flag                   */
  50. short   Count;                          /* Count pages flag                 */
  51.  
  52. FILE    *InFile = NULL;                 /* Disk file where we get text from */
  53. FILE    *Printer = NULL;                /* Printer - AmigaDOS PAR:          */
  54.  
  55. char    FName[80];                      /* File name                        */
  56. char    Scrap[10];                      /* Used to get user input           */
  57. char    ColSpaces[7];                   /* No. of spaces between columns    */
  58.  
  59.                                        /* The init codes for Epson printers */
  60. char    Page_Init[] = "\x1B3\x0F\x1BQ\xFF\x1BM\x0F\x1BS0\x1BU1";
  61. /*
  62. ** Here's what the codes mean so you can change them for some non-Epson printer
  63. ** Note: $1B is written as the mnemonic <esc>
  64. **
  65. ** <esc>3<$0F>      - sets spacing to 15 ($F) 216th's of an inch
  66. ** <esc>Q<$FF>      - clears right margin - sets to "infinite" 255
  67. ** <esc>M           - sets "elite" pitch (96 chars / line on my printer)
  68. ** <$0F>            - sets condensed print mode (cond. elite: 160 chars/line)
  69.  
  70. ** <esc>S0          - sets superscript char set
  71. ** <esc>U1          - sets unidirectional mode - for perfect alignment
  72. **                    (bidirectional wouldn't speed things up anyway)
  73. */
  74.  
  75.  
  76. char    *HelpText[] =
  77.     {
  78.     "\nUse:   TType  [filename]  [-C<columns>]  [-F]  [-T<tab>]  [-S<page>]",
  79.     "              [-L<page>]  [-O]  [-E]  [-P]  [-Q]",
  80.     "Options:",
  81.     "        -C<columns> : sets no. of columns printed - 1 to 8  (default 2)",
  82.     "        -F          : supresses printing of file name and page #",
  83.     "        -T<tab>     : sets tab size - 3 to 16               (default 8)",
  84.     "        -S<page>    : sets first page to print              (default 1)",
  85.     "        -L<page>    : sets last page to print             (default EOF)",
  86.     "        -O          : print odd numbered pages",
  87.     "        -E          : print even numbered pages",
  88.     "        -P          : give total no. of TinyType pages in document. ",
  89.     "                      Doesn't print anything. Uses -C and -T options to",
  90.     "                      determine page format.",
  91.     "        -Q          : sets query mode - asks after each page whether or",
  92.     "                      not to continue.  This overrides the Ending Page.",
  93.     "",
  94.     "Only EPSON-compatible printers are supported.  This program bypasses the",
  95.     "AmigaDOS  PRT:  device and writes  to the  parallel port  directly.  All",
  96.     "unprintable (ASCII codes 128 - 255) as well  as unused control codes are",
  97.     "filtered out.  The only control codes used are CR and LF, and TAB..",
  98.     NULL
  99.     };
  100.  
  101.  
  102.  
  103. void main(argc,argv)
  104. int argc;                               /* Not short.. Might be 32-bit int  */
  105. char *argv[];
  106.     {
  107.     short i,Tmp;
  108.  
  109.     printf("TinyType V1.30\n");
  110.     printf("By Christopher Weber (c)1991 - see docs for distribution policy\n");
  111.  
  112.     srand((short)time(NULL));
  113.  
  114.     if((* argv[1] == '?') || (argc == 1))            /* Print help text                  */
  115.         HelpTxt();
  116.  
  117.     PrintFooter = 1;                    /* Set up defaults                  */
  118.     NumColumns = 2;
  119.     TabSize = 8;
  120.     StartPage = 1;
  121.     EndPage = -1;
  122.     Query = 0;
  123.     Odd = 0;
  124.     Even = 0;
  125.     Count = 0;
  126.  
  127.     for(i=1;i<argc;i++)                 /* Look through arguments           */
  128.         {
  129.         if(argv[i][0] == '-')           /* Control codes maybe..            */
  130.             switch(toupper(argv[i][1])) /* Let's look at code               */
  131.                 {
  132.                 case 'C':
  133.                     Tmp = atoi(& argv[i][2]);
  134.                     if(Tmp >= 1 && Tmp <= 8)
  135.                         NumColumns = Tmp;
  136.                     break;
  137.                 case 'F':
  138.                     PrintFooter = 0;
  139.                     break;
  140.                 case 'T':
  141.                     Tmp = atoi(& argv[i][2]);
  142.                     if(Tmp >= 3 && Tmp <= 16)
  143.                         TabSize = Tmp;
  144.                     break;
  145.                 case 'S':
  146.                     StartPage = atoi(& argv[i][2]);
  147.                     break;
  148.                 case 'L':
  149.                     EndPage = atoi(& argv[i][2]);
  150.                     break;
  151.                 case 'O':
  152.                     Odd = 1;
  153.                     break;
  154.                 case 'E':
  155.                     Even = 1;
  156.                     break;
  157.                 case 'Q':
  158.                     Query = 1;
  159.                     break;
  160.                 case 'P':
  161.                     Count = 1;
  162.                     break;
  163.                 default:    /* Invalid option - inform user */
  164.                     {
  165.                     printf("\nInvalid option : -%c\n",toupper(argv[i][1]));
  166.                     HelpTxt();
  167.                     }
  168.                 }
  169.  
  170.         else if(InFile == NULL)  /* If no file yet, try to open this */
  171.             {
  172.             InFile = fopen(argv[i],"r");
  173.             strcpy(FName,argv[i]);
  174.             }
  175.        }
  176.  
  177.        if(InFile==NULL)   /* User gave us an invalid file or no file.. */
  178.             {
  179.             if(FName == NULL)
  180.                 printf("\nNo File name given.\n");
  181.             else
  182.                 printf("\nFile \"%s\" not found.\n",FName);
  183.             HelpTxt();
  184.             }
  185.  
  186.  
  187. /* Figure out page layout before we do anything. */
  188.  
  189.     if(NumColumns >= 2)                 /* If several columns               */
  190.         {
  191.         ColWidth = (COLUMNS / NumColumns) - 1;
  192.  
  193. /*
  194. ** Here, we have a problem.  If the user asked for some weird # of columns
  195. ** like 7, the columns will have strange widths, and an odd number of spaces.
  196. ** This little routine accounts for all the extra spaces, and then distributes
  197. ** them among the columns, as evenly as possible.  That's all there is to this
  198. ** ugly contraption..
  199. */
  200.  
  201.         Tmp = COLUMNS - (ColWidth * NumColumns);
  202.  
  203.         for(i=0;i<NumColumns-1;i++)     /* Distribute integer spaces        */
  204.             ColSpaces[i] = Tmp / (NumColumns - 1);
  205.  
  206.                                         /* Subtract # already taken up.     */
  207.         Tmp -= (NumColumns - 1) * ColSpaces[0];
  208.  
  209.         for(i=0;i<Tmp;i++)              /* Randomly distribute the rest..   */
  210.             ColSpaces[rand() % (NumColumns-1)] ++;
  211.         }
  212.     else
  213.         {
  214.         ColWidth = COLUMNS;             /* 1 column - one BIG column..      */
  215.  
  216.         for(i=0;i<8;i++)                /* No spaces at all..               */
  217.             ColSpaces[i] = 0;
  218.         }
  219.  
  220.                                         /* IT WORX!!!                       */
  221.  
  222. /* Ok. We know the page layout - now to actually do something. */
  223.  
  224.     if(Count)                    /* Count total number of pages in doc */
  225.         {                             /* but don't print anything.          */
  226.          do                           /* This will ignore Start and End     */
  227.            {                          /* values.                            */
  228.            ReadPage();
  229.            Pages++;
  230.            } while(! feof(InFile));
  231.  
  232.            printf("\nTotal # of TType pages in doc. are : %d\n", Pages - 1);
  233.            fclose(InFile);
  234.            exit(0);
  235.         }
  236.  
  237. /* Is the Ending page number legal? */
  238.  
  239.     if((EndPage != -1) && (EndPage < StartPage))
  240.         {
  241.         printf("\nWarning.  The Ending Page no. is illegal - has been set to\n");
  242.         printf("EOF.  Proceed? (Y/N) :");
  243.         gets(Scrap);
  244.         if(toupper(Scrap[0]) == 'Y')
  245.             EndPage = -1;
  246.         else
  247.             {
  248.             fclose(InFile);
  249.             printf("Exiting.\n");
  250.             exit(0);
  251.             }
  252.         }
  253.  
  254.     if(Even) /* If we're printing even numbered pages */
  255.         {
  256.         Pages = 2;                /* set page count to reflect even pages   */
  257.         ReadPage();               /* skip first page                        */
  258.         if (StartPage == 1)
  259.             StartPage = 2;        /* Then the default first page is changed */
  260.         if (EndPage == -1)
  261.             EndPage = -2;         /* and the end page is changed            */
  262.         }
  263.  
  264.  
  265. /* Are any Start and End values given correct for print mode we are using? */
  266.  
  267.      if( (Odd && ((EndPage%2 == 0) || (StartPage%2==0))) ||
  268.          (Even && ((EndPage%2 != 0) || (StartPage%2 != 0))) )
  269.         {
  270.         printf("\nWarning.  The Ending or Starting Page no. is illegal.\n");
  271.         printf("Starting and Ending page numbers must be ");
  272.         if(Odd)
  273.             printf("odd.\n");
  274.         else
  275.             printf("even.\n");
  276.         fclose(InFile);
  277.         exit(0);
  278.  
  279.         }
  280.  
  281.     Printer = fopen("PAR:","w");        /* Try to open printer              */
  282.     if(Printer == NULL)                 /* Not available...                 */
  283.         {
  284.         fclose(InFile);
  285.         printf("\nParallel printer not available.\n\n");
  286.         Delay(40L);
  287.         exit(0);
  288.         }
  289.  
  290.     fprintf(Printer,"\x1B@");           /* Reset printer..                  */
  291.  
  292.     printf("\nWorking.\n");
  293.  
  294.     printf("\nPrinting %d chars on %d columns.\n",ColWidth,NumColumns);
  295.  
  296.     if(TabSize > ColWidth)
  297.         {
  298.         printf("\nWarning.  Tab size is greater than column width.  It has\n");
  299.         printf("been reduced to 8.  Proceed? (Y/N) :");
  300.         gets(Scrap);
  301.         if(toupper(Scrap[0]) == 'Y')
  302.             TabSize = 8;
  303.         else
  304.             {
  305.             fclose(InFile);
  306.             fclose(Printer);
  307.             exit(0);
  308.             }
  309.         }
  310.  
  311.     do                                  /* Print a page each time through   */
  312.         {
  313.         printf("Reading Page %d : ",Pages);
  314.         fflush(stdout);                 /* Force printing of message above  */
  315.  
  316.         ReadPage();                     /* Load a page                      */
  317.  
  318.         if(Pages >= StartPage)          /* Print this page?                 */
  319.             {
  320.             printf("Printing.\n");
  321.             PrintPage();                /* Yes..                            */
  322.             }
  323.         else
  324.             printf("Bypassing.\n");     /* Ignore this page..               */
  325.  
  326.  
  327.         if(Query)                       /* Well, query...                   */
  328.             {
  329.             printf("Continue on to page ");
  330.             if (Odd || Even)
  331.                 printf("%d? (Y/N) :",Pages+2);
  332.             else
  333.                 printf("%d? (Y/N) :",Pages+1);
  334.             gets(Scrap);
  335.             if(toupper(Scrap[0]) == 'N')
  336.                 break;
  337.             }
  338.         else if(Pages == EndPage)       /* User don't wanna see no more..   */
  339.             {
  340.             printf("Preset last page done.\n");
  341.             break;
  342.             }
  343.  
  344.         if(Odd || Even) /* If we are only printing Odd or Even */
  345.             {                         /* pages then we need to skip a page.  */
  346.             Pages = Pages + 2;
  347.             ReadPage();                     /* Load a page and skip it     */
  348.             }
  349.         else
  350.             Pages++;                        /* Increment page counter           */
  351.  
  352.         } while(! feof(InFile));        /* Go on, ad inifinitum..           */
  353.  
  354.     fprintf(Printer,"\x1B@");           /* Reset printer                    */
  355.  
  356.     printf("\nFinished.\n");
  357.  
  358.     fclose(Printer);                    /* Close open files                 */
  359.     fclose(InFile);
  360.     }
  361.  
  362.  
  363. void PrintPage()
  364.     {
  365.     short i,x;
  366.  
  367.     fprintf(Printer,"%s",Page_Init);    /* Init printer for page            */
  368.  
  369.     for(i=0;i<LINES;i++)                /* Print all lines                  */
  370.            fprintf(Printer,"%s", PText[i]);
  371.  
  372.     fprintf(Printer,"\n");              /* Leave a blank line               */
  373.  
  374.     if(PrintFooter)                     /* Conditionally print footer       */
  375.         fprintf(Printer,"[TType]  \x0EFile \"%s\"  Page %d",FName,Pages);
  376.  
  377.     fprintf(Printer,"\x0C");            /* Go to next page - form feed      */
  378.     }
  379.  
  380. void ReadPage()
  381.     {
  382.     short   x,y,col;                    /* Co-ordinates of "cursor"         */
  383.     unsigned char   c;
  384.     short   XStart, XEnd;               /* Column co-ordinates              */
  385.  
  386.  
  387.     for(y=0;y<LINES;y++)                /* Clear page totally               */
  388.         {
  389.         PText[y][COLUMNS + 1] = '';     /* NULL terminate each line */
  390.         for(x=0;x<COLUMNS;x++)
  391.             {
  392.             PText[y][x] = ' ';
  393.             }
  394.         }
  395.  
  396.     x = y = col = 0;
  397.     XStart = 0;
  398.     XEnd = ColWidth-1;
  399.  
  400.     while(1)
  401.         {
  402.         c = getc(InFile);               /* Read a character from the file   */
  403.  
  404.         if(feof(InFile))                /* Ugly check for EOF..  Die Fast!  */
  405.             return;
  406.  
  407.         switch(c)                       /* Let us parse..                   */
  408.             {
  409.             case '\n':
  410.             case '\r':
  411.                 x = XStart;             /* Go to next line / column / page  */
  412.                 y ++;
  413.                 break;
  414.  
  415.             case '\t':
  416.                 do
  417.                     {
  418.                     x++;
  419.                     } while((x % TabSize) && (x < XEnd));
  420.                 break;
  421.  
  422.             default:
  423.                 if(isprint(c))          /* REAL text!...  Aaahhhh           */
  424.                     {
  425.                     PText[y][x] = c;    /* Put it in array, advance cursor  */
  426.                     x ++;
  427.  
  428.                     if(x > XEnd)        /* Remove excess CR / LFs           */
  429.                         {
  430.                         c = getc(InFile);
  431.                         if(c!='\n' && c!='\r')
  432.                             ungetc(c,InFile);
  433.                         }
  434.                     }
  435.             }
  436.  
  437.         if(x > XEnd)                    /* If at end of column width        */
  438.             {
  439.             y++;                        /* Next line!                       */
  440.             x = XStart;
  441.             }
  442.  
  443.         if(y == LINES)
  444.             {
  445.             col ++;                     /* Next column! - adjust XStart/End */
  446.  
  447.             if(col == NumColumns)
  448.                 return;                 /* Finished all columns.  Return.   */
  449.  
  450.             y = 0;                      /* Top of column                    */
  451.  
  452.                                         /* Calculate new XStart             */
  453.             XStart += ColWidth + ColSpaces[col-1];
  454.  
  455.             XEnd = XStart + ColWidth-1; /* Calculate new XEnd               */
  456.  
  457.             x = XStart;
  458.             }
  459.         }
  460.     }
  461.  
  462. void HelpTxt()
  463.     {
  464.     short i;
  465.  
  466.     for(i=0;HelpText[i];i++)
  467.     printf("%s\n",HelpText[i]);
  468.     exit(0);
  469.     }
  470.